package pl.edu.agh.jemo.evolution.objfunc.impl.weighted;

import javax.swing.JPanel;

import org.apache.log4j.Logger;

import pl.edu.agh.jemo.evolution.objfunc.ClassicObjectiveFunction;
import pl.edu.agh.jemo.evolution.objfunc.CrowdingObjectiveFunction;
import pl.edu.agh.jemo.evolution.specimen.Specimen;
import pl.edu.agh.jemo.gui.LoggerHelper;


public class WeightedCrowdingObjectiveFunction extends CrowdingObjectiveFunction {
	
	private CrowdingObjectiveFunction mainFunction;
	private ClassicObjectiveFunction weighingFunction;
	private Double weighingFunctionWorstValue;
	private Logger logger = LoggerHelper.getInstance().getLogger();
	
	@Override
	public Double calculateValue(Specimen specimen) {
		final Double weighingFunctionValue = weighingFunction.calculateValue(specimen);
		final Double factor = 1d + Math.abs(weighingFunctionValue) / (3 * weighingFunctionWorstValue);
		final Double mainFunctionValue = mainFunction.calculateValue(specimen);
		return factor * mainFunctionValue;
	}
	
	public WeightedCrowdingObjectiveFunction(CrowdingObjectiveFunction mainFunction, ClassicObjectiveFunction weighingFunction, Double weighingFunctionMaxValue) {
		this.mainFunction = mainFunction;
		this.weighingFunction = weighingFunction;
		this.weighingFunctionWorstValue = weighingFunctionMaxValue;	
	}
	
	@Override
	protected void populationChanged() {
		mainFunction.setPopulationReference(population);
	}

	@Override
	public JPanel getConfigPanel() {
		return null;
	}

	@Override
	public Double getMaxValue() {
		return mainFunction.getMaxValue();
	}
	
	@Override
	public Double getMinValue() {
		return mainFunction.getMinValue();
	}
}
